package lx.exalarm.widget;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.NumberPicker;

import lx.exalarm.R;

/**
 * Created by liuxu on 15-3-4.
 */
public class PickerDialog extends AlertDialog
        implements View.OnClickListener {

    private NumberPicker mPicker;
    private int mValue;
    private int mMin;
    private int mMax;
    private NumberPicker.Formatter mFormatter;
    private ConfirmListener mConfirmListener;

    public PickerDialog(Context context, int value, int min, int max,
                         NumberPicker.Formatter formatter,
                         ConfirmListener confirmListener) {
        super(context);
        mValue = value;
        mMax = max;
        mMin = min;
        mFormatter = formatter;
        mConfirmListener = confirmListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dlg_picker);

        Window dialogWindow = getWindow();
        dialogWindow.setLayout(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT);
        dialogWindow.setGravity(Gravity.CENTER);

        mPicker = (NumberPicker) findViewById(R.id.dlg_picker_np);
        mPicker.setFormatter(mFormatter);
        mPicker.setMinValue(mMin);
        mPicker.setMaxValue(mMax);
        mPicker.setValue(mValue);
        findViewById(R.id.dlg_picker_confirm).setOnClickListener(this);
        findViewById(R.id.dlg_picker_cancel).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dlg_picker_cancel: {
                dismiss();
                break;
            }
            case R.id.dlg_picker_confirm: {
                if (mConfirmListener != null) {
                    mConfirmListener.onConfirm(mPicker.getValue());
                }
                dismiss();
                break;
            }
        }
    }

    public static interface ConfirmListener {
        public void onConfirm(int value);
    }
}
